package Day26;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

public class Demo02 {
    //XML解析本地文件
    public static void main(String[] args) throws IOException, DocumentException {
        FileInputStream fis = new FileInputStream("C:\\Users\\刘娟\\IdeaProjects\\basic-code\\cn.itcast.kaikeba\\src\\TuShu.xml");
        //因为XML的解析需要dom4j.jar文件需要导入

        //创建XML读取对象
        SAXReader sr = new SAXReader();
        //读取并得到文档对象
        Document doc = sr.read(fis);
        //通过文档获取根元素
        Element root = doc.getRootElement();
        //开始解析元素
        System.out.println(root.getName());//得到根元素的名字   books
//        Element book = root.element("book");
//        Element name = book.element("name");
//        System.out.println(name.getText());//金苹果

        List<Element> es = root.elements();
        for (int i = 0; i < es.size(); i++) {
            Element book = es.get(i);
            System.out.println(book.attributeValue("id"));
            System.out.println(book.elementText("name"));
            System.out.println(book.elementText("info"));
            System.out.println("----------------------------");
           //1001
            //金苹果
            //锄禾日当午
            //----------------------------
            //1002
            //银苹果
            //汗滴禾下土
            //----------------------------
        }


        fis.close();



    }

}
